// HSS.Forms.Docking.DockModuleBase.cs
// ----------------------------------------------------------------------------
// Licensed under the MIT license
// http://www.opensource.org/licenses/mit-license.html
// ----------------------------------------------------------------------------
// HighSpeed-Solutions, LLC
// Copyright (c) 2001-2010
// ----------------------------------------------------------------------------
// File:       DockModuleBase.cs
// Author:     HSS\gbanta
// Created:    08/12/2010
// Modified:   12/04/2010
// ----------------------------------------------------------------------------
namespace HSS.Forms.Docking
{
	#region Using Directives
	using System;
	using System.Collections;
	using System.Collections.Generic;
	using System.ComponentModel;
	using System.Data;
	using System.Drawing;
	using System.Windows.Forms;
	using HSS.Data;
	#endregion

	#region DockModuleBase
	/// <summary>
	/// DockModuleBase is a <see cref="HSS.Forms.Docking.DockContent"/> Module for displaying data
	/// </summary>
	[DefaultEvent("Dirty")]
	[DefaultProperty("UniqueID")]
	[Description("DockModuleBase is a HSS.Forms.Docking.DockContent Module for displaying data.")]
	public class DockModuleBase22 : DockContent, ICanBeDirty
	{
		#region Constants
		/// <summary>
		/// Represents the default text in a ComboBox
		/// </summary>
		public const string ComboBoxSelectText = "<Select Item>";
		/// <summary>
		/// Represents the default value in a ComboBox
		/// </summary>
		public const string ComboBoxSelectValue = "selectitem";
		/// <summary>
		/// Represents the Configuration is not available text for a ComboBox
		/// </summary>
		public const string PropertyNotAvailable = "N/A";
		#endregion

		#region Constructor
		/// <summary>
		/// CTOR
		/// </summary>
		public DockModuleBase22()
		{
		}
		#endregion

		#region Properties

		#region Nonbrowseable
		/// <summary>
		/// Gets or sets whether or not the data on the form is considered dirty.
		/// </summary>
		[Description("Gets or sets whether or not the data on the form is considered dirty.")]
		[Browsable(false)]
		[DefaultValue(false)]
		public bool IsDirty
		{
			get { return _isDirty; }
			set { _isDirty = value; }
		} bool _isDirty;
		/// <summary>
		/// Indicates if this formhas been loaded
		/// </summary>
		[Description("Indicates if this form has been loaded.")]
		[Browsable(false)]
		[DefaultValue(false)]
		public bool Loaded
		{
			get { return _loaded; }
			set { _loaded = value; }
		} bool _loaded;
		/// <summary>
		/// Indicates if this form is currently loading
		/// </summary>
		[Description("Indicates if this form is currently loading.")]
		[Browsable(false)]
		[DefaultValue(false)]
		public bool Loading
		{
			get { return _loading; }
			set { _loading = value; }
		} bool _loading;
		/// <summary>
		/// Indicates if this form is currently updating
		/// </summary>
		[Description("Indicates if this form is currently updating.")]
		[Browsable(false)]
		[DefaultValue(false)]
		public bool Updating
		{
			get { return _updating; }
			set { _updating = value; }
		} bool _updating;
		/// <summary>
		/// Contains the detail data for this form
		/// </summary>
		[Description("Contains the data for this form.")]
		[Browsable(false)]
		public IDataContainer Data
		{
			get { return _data; }
			set { _data = value; }
		} IDataContainer _data;
		/// <summary>
		/// The Primary KEY for the data currently loaded in the Module
		/// </summary>
		[Description("The Unique Id that identifies the data currently loaded in the Module. Default is -1.")]
		[Browsable(false)]
		[DefaultValue("-1")]
		public string UniqueID
		{
			get { return _uniqueID; }
			set { _uniqueID = value; }
		} string _uniqueID = "-1";
		#endregion

		#region Behavior
		/// <summary>
		/// Indicates if the module allows editing
		/// </summary>
		[Description("Indicates if the module allows editing.")]
		[CategoryAttribute("Behavior")]
		[DefaultValue(true)]
		public bool AllowEdit
		{
			get { return _allowEdit; }
			set
			{
				if (_allowEdit != value)
				{
					_allowEdit = value;
					InvokeAllowEditChanged(this, EventArgs.Empty);
				}
			}
		} bool _allowEdit = true;
		#endregion

		#endregion

		#region Methods
		/// <summary>
		/// Virtual Method for initializing the form by setting the UniqueId of the data for this form.
		/// </summary>
		/// <param name="UniqueID">The primary key associated with the data contained on the form</param>
		/// <returns>true if the Form successfully initialized; else false</returns>
		public virtual bool InitializeModule(string UniqueID)
		{
			this.UniqueID = UniqueID;
			return true;
		}
		/// <summary>
		/// Virtual Method for initializing the form, setting the data and calling the LoadData method.
		/// </summary>
		/// <param name="Data">The IDataContainer to load</param>
		/// <returns>true if the Form successfully initialized; else false</returns>
		public virtual bool InitializeModule(IDataContainer Data)
		{
			this._data = Data;
			if (null != Data)
				this.UniqueID = Data.Id;
			this.LoadData();
			return true;
		}
		/// <summary>
		/// Virtual Method for the loading data into the form. Is called by default from InitializeModule
		/// unless InitializeModule has been overloaded.
		/// </summary>
		public virtual void LoadData()
		{
		}
		/// <summary>
		/// Virtual Method for the extracting the data from the form's controls and placing into the <see cref="Data"/> property.
		/// </summary>
		public virtual void GetData()
		{
		}
		/// <summary>
		/// Virtual Method for saving the data on the form
		/// </summary>
		/// <returns>true if the Save operation was sucessful; otherwise false</returns>
		public virtual bool SaveData()
		{
			return false;
		}
		/// <summary>
		/// Virtual Method for the reseting the form and it's child controls
		/// </summary>
		public virtual void Reset()
		{
		}
		/// <summary>
		/// Set Module to a Dirty State
		/// </summary>
		protected virtual void SetDirty()
		{
			this.SetDirty(true);
		}
		/// <summary>
		/// Set Module to a Dirty State
		/// </summary>
		/// <param name="IsEditing">Indicates user has begun editing fields</param>
		protected virtual void SetDirty(bool IsEditing)
		{
			this.SetDirty(false, IsEditing, null);
		}
		/// <summary>
		/// Set Module to a Dirty State
		/// </summary>
		/// <param name="ForceDirty">Force setting Module to dirty and raise event.</param>
		/// <param name="IsEditing">Indicates user has begun editing fields</param>
		protected virtual void SetDirty(bool ForceDirty, bool IsEditing)
		{
			this.SetDirty(ForceDirty, IsEditing, null);
		}
		/// <summary>
		/// Set Module to a Dirty State
		/// </summary>
		/// <param name="ForceDirty">Force setting Module to dirty and raise event.</param>
		/// <param name="IsEditing">Indicates user has begun editing fields</param>
		/// <param name="State">State object</param>
		protected virtual void SetDirty(bool ForceDirty, bool IsEditing, object State)
		{
			if (!this.IsDirty | ForceDirty)
				this.IsDirty = true;
			InvokeDirty(this, new DirtyEventArgs(this.IsDirty, IsEditing, State));
		}
		/// <summary>
		/// Set Modules Dirty State
		/// </summary>
		/// <param name="IsDirty">Indicates if form is dirty</param>
		/// <param name="State">State object</param>
		protected virtual void SetDirty(bool IsDirty, object State)
		{
			this.IsDirty = IsDirty;
			InvokeDirty(this, new DirtyEventArgs(IsDirty, true, State));
		}
		/// <summary>
		/// Validate date before saving
		/// </summary>
		/// <returns>Boolean indicating if Data is valid</returns>
		protected virtual bool ValidateData(ref string ErrorMessage) { return true; }
		#endregion

		#region MessageBox Helper
		/// <summary>
		/// Display a message box
		/// </summary>
		/// <param name="message">The message to display</param>
		public DialogResult MsgBox(string message)
		{
			return MsgBox(message, MessageBoxButtons.OK, MessageBoxIcon.Error);
		}
		/// <summary>
		/// Display a message box
		/// </summary>
		/// <param name="message">The message to display</param>
		/// <param name="caption">The caption for the window</param>
		public DialogResult MsgBox(string message, string caption)
		{
			return MsgBox(message, caption, MessageBoxButtons.OK, MessageBoxIcon.Error);
		}
		/// <summary>
		/// Display a message box
		/// </summary>
		/// <param name="message">The message to display</param>
		/// <param name="button">The button to display</param>
		/// <param name="icon">The icon to display</param>
		public DialogResult MsgBox(string message, MessageBoxButtons button, MessageBoxIcon icon)
		{
			return MsgBox(message, Application.ProductName, button, icon);
		}
		/// <summary>
		/// Display a message box
		/// </summary>
		/// <param name="message">The message to display</param>
		/// <param name="caption">The caption for the window</param>
		/// <param name="button">The button to display</param>
		/// <param name="icon">The icon to display</param>
		public DialogResult MsgBox(string message, string caption, MessageBoxButtons button, MessageBoxIcon icon)
		{
			return MessageBox.Show(message, caption, button, icon);
		}

		#endregion

		#region ComboBox Helper

		/// <summary>
		/// InitializeComboBox
		/// </summary>
		/// <param name="ctl">The ComboBox to initialize</param>
		/// <param name="dt">The DataTable to use as the datasource</param>
		/// <param name="DisplayColumn">The Column Name that should be displayed</param>
		/// <param name="ValueColumn">The Column Name that should be stored</param>
		/// <param name="SetDefault">Whether or not to set the list to a default list item</param>
		public static void InitializeComboBox(ComboBox ctl,
			DataTable dt,
			string DisplayColumn,
			string ValueColumn,
			bool SetDefault)
		{

			if (null == ctl)
				return;

			List<ValueMember> vm = new List<ValueMember>();

			if (null != dt)
			{
				foreach (System.Data.DataRow r in dt.Rows)
					vm.Add(new ValueMember(r[DisplayColumn].ToString(), r[ValueColumn].ToString()));
			}

			if (vm.Count == 0)
			{
				ctl.ValueMember = null;
				ctl.DisplayMember = null;
				ctl.DataSource = null;
			}
			else
			{

				if (ctl.ValueMember != "Value")
					ctl.ValueMember = "Value";

				if (ctl.DisplayMember != "TextDisplay")
					ctl.DisplayMember = "TextDisplay";

				ctl.DataSource = vm.ToArray().Clone();

				if (SetDefault)
					ctl.SelectedIndex = 0;
				else
					ctl.SelectedIndex = -1;
			}

			ctl.Refresh();
		}

		/// <summary>
		/// InitializeComboBox
		/// </summary>
		/// <param name="ctl">The ComboBox to initialize</param>
		/// <param name="dt">The DataTable to use as the datasource</param>
		/// <param name="DisplayColumn">The Column Name that should be displayed</param>
		/// <param name="ValueColumn">The Column Name that should be stored</param>
		public static void InitializeComboBox(ComboBox ctl,
			DataTable dt,
			string DisplayColumn,
			string ValueColumn)
		{

			if (null == ctl)
				return;

			InitializeComboBox(ctl, dt, DisplayColumn, ValueColumn, true);

		}

		/// <summary>
		/// InitializeComboBox
		/// </summary>
		/// <param name="ctl">The ComboBox to initialize</param>
		/// <param name="dt">The DataTable to use as the datasource</param>
		/// <param name="PreserveSelectValue">Preserve the previous selected item</param>
		/// <param name="DisplayColumn">The Column Name that should be displayed</param>
		/// <param name="ValueColumn">The Column Name that should be stored</param>
		public static void InitializeComboBox(ComboBox ctl,
			DataTable dt,
			bool PreserveSelectValue,
			string DisplayColumn,
			string ValueColumn)
		{

			if (null == ctl)
				return;

			ValueMember ExistingValue = null;
			if (PreserveSelectValue && null != ctl.SelectedValue)
				ExistingValue = new ValueMember(ctl.SelectedText, ctl.SelectedValue.ToString());

			List<ValueMember> vm = new List<ValueMember>();

			if (null != dt)
			{
				foreach (System.Data.DataRow r in dt.Rows)
					vm.Add(new ValueMember(r[DisplayColumn].ToString(), r[ValueColumn].ToString()));
			}

			if (vm.Count == 0)
			{
				ctl.ValueMember = null;
				ctl.DisplayMember = null;
				ctl.DataSource = null;
			}
			else
			{

				if (ctl.ValueMember != "Value")
					ctl.ValueMember = "Value";

				if (ctl.DisplayMember != "TextDisplay")
					ctl.DisplayMember = "TextDisplay";

				ctl.DataSource = vm.ToArray().Clone();

				bool found = false;
				if (PreserveSelectValue && (null != ExistingValue))
				{
					foreach (object item in ctl.Items)
					{
						if (((ValueMember)item).Value == ExistingValue.Value)
						{
							ctl.SelectedValue = ExistingValue.Value;
							found = true;
							break;
						}
					}
					if (!found)
					{
						ctl.SelectedValue = ((ValueMember)ctl.Items[0]).Value;
						found = true;
					}
				}
				if (!found)
				{
					ctl.SelectedIndex = 0;
				}
			}

			ctl.Refresh();
		}

		/// <summary>
		/// InitializeComboBox
		/// </summary>
		/// <param name="ctl">The ComboBox to initialize</param>
		/// <param name="dr">The List to use as the datasource</param>
		public static void InitializeComboBox(ComboBox ctl,
			IDictionary dr)
		{

			if (null == ctl)
				return;

			List<ValueMember> vm = new List<ValueMember>();

			if (null != dr)
			{
				foreach (DictionaryEntry de in dr)
					vm.Add(new ValueMember(de.Value.ToString(), de.Key.ToString()));
			}

			if (vm.Count == 0)
			{
				ctl.ValueMember = null;
				ctl.DisplayMember = null;
				ctl.DataSource = null;
			}
			else
			{

				if (ctl.ValueMember != "Value")
					ctl.ValueMember = "Value";

				if (ctl.DisplayMember != "TextDisplay")
					ctl.DisplayMember = "TextDisplay";

				ctl.DataSource = vm.ToArray().Clone();
			}

			ctl.Refresh();
		}

		/// <summary>
		/// InitializeComboBox
		/// </summary>
		/// <param name="ctl">The ComboBox to initialize</param>
		/// <param name="dr">The List to use as the datasource</param>
		/// <param name="ShowSelect">Show select</param>
		public static void InitializeComboBox(ComboBox ctl,
			IDictionary dr,
			bool ShowSelect)
		{

			if (null == ctl)
				return;

			List<ValueMember> vm = new List<ValueMember>();

			if (null != dr)
			{
				if (ShowSelect)
					vm.Add(new ValueMember(ComboBoxSelectText, ComboBoxSelectValue));

				foreach (DictionaryEntry de in dr)
					vm.Add(new ValueMember(de.Value.ToString(), de.Key.ToString()));
			}

			if (vm.Count == 0)
			{
				ctl.ValueMember = null;
				ctl.DisplayMember = null;
				ctl.DataSource = null;
			}
			else
			{

				if (ctl.ValueMember != "Value")
					ctl.ValueMember = "Value";

				if (ctl.DisplayMember != "TextDisplay")
					ctl.DisplayMember = "TextDisplay";

				ctl.DataSource = vm.ToArray().Clone();
			}

			ctl.Refresh();
		}

		/// <summary>
		/// InitializeComboBox
		/// </summary>
		/// <param name="ctl">The ComboBox to initialize</param>
		/// <param name="dr">The DataRow array to use as the datasource</param>
		/// <param name="DisplayIndex">The index of the column to Display</param>
		/// <param name="ValueIndex">The index of the column to use for the Value</param>
		public static void InitializeComboBox(ComboBox ctl,
			DataRow[] dr,
			int DisplayIndex,
			int ValueIndex)
		{

			if (null == ctl)
				return;

			List<ValueMember> vm = new List<ValueMember>();

			if (null != dr)
			{
				foreach (DataRow de in dr)
					vm.Add(new ValueMember(de.ItemArray[DisplayIndex].ToString(), de.ItemArray[ValueIndex].ToString()));
			}

			if (vm.Count == 0)
			{
				ctl.ValueMember = null;
				ctl.DisplayMember = null;
				ctl.DataSource = null;
			}
			{

				if (ctl.ValueMember != "Value")
					ctl.ValueMember = "Value";

				if (ctl.DisplayMember != "TextDisplay")
					ctl.DisplayMember = "TextDisplay";

				ctl.DataSource = vm.ToArray().Clone();
			}

			ctl.Refresh();
		}


		/// <summary>
		/// InitializeComboBox
		/// </summary>
		/// <param name="ctl">The ComboBox to initialize</param>
		/// <param name="dr">The DataRowReader to use as the datasource</param>
		/// <param name="DisplayName">The index of the column to Display</param>
		/// <param name="ValueName">The index of the column to use for the Value</param>
		public static void InitializeComboBox(ComboBox ctl,
			DataRowReader dr,
			string DisplayName,
			string ValueName)
		{

			if (null == ctl)
				return;

			List<ValueMember> vm = new List<ValueMember>();

			if (null != dr)
			{
				while (dr.Read())
				{
					vm.Add(new ValueMember(dr[DisplayName].ToString(), dr[ValueName].ToString()));
				}
			}

			if (vm.Count == 0)
			{
				ctl.ValueMember = null;
				ctl.DisplayMember = null;
				ctl.DataSource = null;
			}
			{

				if (ctl.ValueMember != "Value")
					ctl.ValueMember = "Value";

				if (ctl.DisplayMember != "TextDisplay")
					ctl.DisplayMember = "TextDisplay";

				ctl.DataSource = vm.ToArray().Clone();
			}

			ctl.Refresh();
		}

		/// <summary>
		/// InitializeComboBox
		/// </summary>
		/// <param name="ctl">The ComboBox to initialize</param>
		/// <param name="dr">The DataRowReader to use as the datasource</param>
		/// <param name="DisplayName">The index of the column to Display</param>
		/// <param name="ValueName">The index of the column to use for the Value</param>
		/// <param name="AdditionalValues">Additional values</param>
		/// <param name="AdditionalValueKeys">Additional value keys</param>
		public static void InitializeComboBox(ComboBox ctl,
			DataRowReader dr,
			string DisplayName,
			string ValueName,
			string[] AdditionalValues,
			string[] AdditionalValueKeys)
		{

			if (null == ctl)
				return;

			List<ValueMember> vm = new List<ValueMember>();

			if (null != dr)
			{
				if (null != AdditionalValues)
				{
					if (AdditionalValues.Length == AdditionalValueKeys.Length)
					{
						for (int item = 0; item < AdditionalValues.Length; item++)
						{
							vm.Add(new ValueMember(AdditionalValues[item], AdditionalValueKeys[item]));
						}
					}
				}

				while (dr.Read())
				{
					vm.Add(new ValueMember(dr[DisplayName].ToString(), dr[ValueName].ToString()));
				}
			}

			if (vm.Count == 0)
			{
				ctl.ValueMember = null;
				ctl.DisplayMember = null;
				ctl.DataSource = null;
			}
			{

				if (ctl.ValueMember != "Value")
					ctl.ValueMember = "Value";

				if (ctl.DisplayMember != "TextDisplay")
					ctl.DisplayMember = "TextDisplay";

				ctl.DataSource = vm.ToArray().Clone();
			}

			ctl.Refresh();
		}

		/// <summary>
		/// InitializeComboBox
		/// </summary>
		/// <param name="ctl">The ComboBox to initialize</param>
		/// <param name="configData">The IDataContainer array to use as the datasource</param>
		/// <param name="ShowSelect">Add the Select Item as the first Item</param>
		/// <param name="PreserveSelectValue">Preserve the previous selected item</param>
		public static void InitializeComboBox(ComboBox ctl,
			IDataContainer[] configData,
			bool ShowSelect,
			bool PreserveSelectValue)
		{
			InitializeComboBox(ctl, configData, ShowSelect, PreserveSelectValue, null, null);
		}

		/// <summary>
		/// InitializeComboBox
		/// </summary>
		/// <param name="ctl">ComboBox</param>
		/// <param name="configData">ConfigurationDataSource</param>
		/// <param name="ShowSelect">Show select</param>
		/// <param name="PreserveSelectValue">Preserve existing value</param>
		/// <param name="AdditionalValues">Additional values</param>
		/// <param name="AdditionalValueKeys">Additional value keys</param>
		public static void InitializeComboBox(ComboBox ctl,
			IDataContainer[] configData,
			bool ShowSelect,
			bool PreserveSelectValue,
			string[] AdditionalValues,
			string[] AdditionalValueKeys)
		{

			if (null == ctl)
				return;

			ValueMember ExistingValue = null;
			if (PreserveSelectValue && null != ctl.SelectedValue)
				ExistingValue = new ValueMember(ctl.SelectedValue.ToString(), ctl.SelectedValue.ToString());

			List<ValueMember> vm = new List<ValueMember>();

			if (null != configData)
			{
				if (ShowSelect)
					vm.Add(new ValueMember(ComboBoxSelectText, ComboBoxSelectValue));

				if (null != AdditionalValues)
				{
					if (AdditionalValues.Length == AdditionalValueKeys.Length)
					{
						for (int item = 0; item < AdditionalValues.Length; item++)
						{
							vm.Add(new ValueMember(AdditionalValues[item], AdditionalValueKeys[item]));
						}
					}
				}

				foreach (IDataContainer data in configData)
					vm.Add(new ValueMember(data.Id, data.Id));
			}

			if (vm.Count == 0)
			{
				ctl.ValueMember = null;
				ctl.DisplayMember = null;
				ctl.DataSource = null;
			}
			else
			{

				if (ctl.ValueMember != "Value")
					ctl.ValueMember = "Value";

				if (ctl.DisplayMember != "TextDisplay")
					ctl.DisplayMember = "TextDisplay";

				ctl.DataSource = vm.ToArray().Clone();

				bool found = false;
				if (PreserveSelectValue && (null != ExistingValue))
				{
					foreach (object item in ctl.Items)
					{
						if (((ValueMember)item).Value == ExistingValue.Value)
						{
							ctl.SelectedValue = ExistingValue.Value;
							found = true;
							break;
						}
					}
					if (!found)
					{
						ctl.SelectedValue = ((ValueMember)ctl.Items[0]).Value;
						found = true;
					}
				}
				if (!found)
				{
					ctl.SelectedIndex = 0;
				}
			}

			ctl.Refresh();
		}

		#endregion

		#region Child Controls Helper

		/// <summary>
		/// The font to set the label of a de-selected Control
		/// </summary>
		/// <value>Font</value>
		[Category("Appearance")]
		[Description("The font to set the label of a de-selected Control")]
		public Font DeselectedFont
		{
			get
			{
				return _deselectedFont;
			}
			set
			{
				_deselectedFont = value;
			}
		} Font _deselectedFont = new Font("Arial Unicode MS", 8, FontStyle.Regular);

		/// <summary>
		/// The color to set the label of a de-selected Control
		/// </summary>
		/// <value>Color</value>
		[Category("Appearance")]
		[Description("The color to set the label of a de-selected Control")]
		public Color DeselectedForeColor
		{
			get { return _deselectedForeColor; }
			set { _deselectedForeColor = value; }
		} Color _deselectedForeColor = Color.Gray;

		/// <summary>
		/// The font to set the label of a selected Control
		/// </summary>
		/// <value>Font</value>
		[Category("Appearance")]
		[Description("The font to set the label of a selected Control")]
		public Font SelectedFont
		{
			get
			{
				return _selectedFont;
			}
			set
			{
				_selectedFont = value;
			}
		} Font _selectedFont = new Font("Verdana", 9, FontStyle.Regular);

		/// <summary>
		/// The color to set the label of a selected Control
		/// </summary>
		/// <value>Color</value>
		[Category("Appearance")]
		[Description("The color to set the label of a selected Control")]
		public Color SelectedForeColor
		{
			get
			{
				return _selectedForeColor;
			}
			set
			{
				_selectedForeColor = value;
			}
		} Color _selectedForeColor = Color.IndianRed;

		/// <summary>
		/// A List Array of Controls to skip when deactivating and setting label font
		/// </summary>
		[Browsable(false)]
		public System.Collections.Generic.List<Control> ControlsToSkip
		{
			get { return _controlsToSkip; }
			set { _controlsToSkip = value; }
		} System.Collections.Generic.List<Control> _controlsToSkip = new System.Collections.Generic.List<Control>();

		/// <summary>
		/// Reset the Children Controls of the Control Collection Parameter
		/// </summary>
		/// <param name="ParentCollection">The Control Collection to search.</param>
		public void ResetControls(Control.ControlCollection ParentCollection)
		{
			foreach (Control foundControl in ParentCollection)
			{
				if (foundControl.HasChildren)
					ResetControls(foundControl.Controls);
				else
				{
					if (foundControl.GetType() == typeof(TextBox))
						((TextBox)foundControl).Text = string.Empty;
					else if (foundControl.GetType() == typeof(RichTextBox))
						((RichTextBox)foundControl).Text = string.Empty;
					else if (foundControl.GetType() == typeof(DateTimePicker))
						((DateTimePicker)foundControl).Checked = false;
					else if (foundControl.GetType() == typeof(RadioButton))
						((RadioButton)foundControl).Checked = false;
					else if (foundControl.GetType() == typeof(ComboBox))
					{
						if (((ComboBox)foundControl).Items.Count > 0)
							((ComboBox)foundControl).SelectedIndex = 0;
						else
							((ComboBox)foundControl).SelectedIndex = -1;
					}
				}
			}
		}

		/// <summary>
		/// FindControl
		/// </summary>
		/// <param name="ParentCollection">ControlCollection to Search</param>
		/// <param name="ControlToFind">The Control to find</param>
		/// <returns>The Control found else (Control)null</returns>
		public Control FindControl(Control.ControlCollection ParentCollection, Control ControlToFind)
		{
			foreach (Control foundControl in ParentCollection)
			{
				if (foundControl.HasChildren)
				{
					Control tmpControl = FindControl(foundControl.Controls, ControlToFind);
					if (null != tmpControl && tmpControl.Name == ControlToFind.Name)
						return tmpControl;
				}
				else
				{
					if (foundControl.Name == ControlToFind.Name)
						return foundControl;
				}
			}
			return null;
		}

		/// <summary>
		/// DeactivateLabels
		/// </summary>
		public void DeactivateLabels()
		{
			DeactivateLabels(this.Controls);
		}
		/// <summary>
		/// DeactivateLabels
		/// </summary>
		/// <param name="ParentCollection">ControlCollection to Search</param>
		public void DeactivateLabels(Control.ControlCollection ParentCollection)
		{
			bool continueSearching = false;
			foreach (Control foundControl in ParentCollection)
			{
				continueSearching = false;

				foreach (object ctl in this._controlsToSkip)
				{
					if (((Control)ctl).Name == foundControl.Name)
					{
						continueSearching = true;
						break;
					}
				}
				if (!continueSearching)
				{
					if (foundControl.HasChildren)
					{
						DeactivateLabels(foundControl.Controls);
					}
					else
					{
						if (foundControl.GetType() == typeof(Label))
						{
							((Label)foundControl).Font = DeselectedFont;
							((Label)foundControl).ForeColor = DeselectedForeColor;
						}
						else if (foundControl.GetType() == typeof(RadioButton))
						{
							if (!((RadioButton)foundControl).Checked || !((RadioButton)foundControl).Enabled)
							{
								((RadioButton)foundControl).Font = DeselectedFont;
								((RadioButton)foundControl).ForeColor = DeselectedForeColor;
							}
							else
							{
								((RadioButton)foundControl).Font = SelectedFont;
								((RadioButton)foundControl).ForeColor = SelectedForeColor;
							}
						}
						else if (foundControl.GetType() == typeof(CheckBox))
						{
							if (!((CheckBox)foundControl).Checked || !((CheckBox)foundControl).Enabled)
							{
								((CheckBox)foundControl).Font = DeselectedFont;
								((CheckBox)foundControl).ForeColor = DeselectedForeColor;
							}
							else
							{
								((CheckBox)foundControl).Font = SelectedFont;
								((CheckBox)foundControl).ForeColor = SelectedForeColor;
							}
						}
					}
				}
			}
		}

		/// <summary>
		/// SetLabels
		/// </summary>
		/// <param name="SelectedControl">The Control to indicate as active</param>
		/// <remarks>This is for simple use only. Assume all child Controls are children of the Control.Controls. If
		/// you have Panels or other Container Controls that have children Controls, you may want to use the UpdateLabels
		/// as it provide more detail Control on how you activate and deacitvate Control. Also use the UpdateLabels method
		/// if you want to skip Controls. This method acts on all Controls.</remarks>
		public void SetLabels(Control SelectedControl)
		{
			UpdateLabels(this.Controls, SelectedControl);
		}
		/// <summary>
		/// UpdateLabel
		/// </summary>
		/// <param name="ParentControl">ControlCollection to Search</param>
		/// <param name="SelectedControl">The Control label to update</param>
		public void UpdateLabels(Control.ControlCollection ParentControl, Control SelectedControl)
		{
			DeactivateLabels(ParentControl);

			Control foundControl = FindControl(ParentControl, SelectedControl);

			if (null != foundControl)
			{
				if (foundControl.GetType() == typeof(Label))
				{
					Label c = (Label)SelectedControl;
					c.Font = SelectedFont;
					c.ForeColor = SelectedForeColor;
				}
				else if (foundControl.GetType() == typeof(RadioButton))
				{
					RadioButton c = (RadioButton)SelectedControl;
					c.Font = SelectedFont;
					c.ForeColor = SelectedForeColor;
				}
				else if (foundControl.GetType() == typeof(CheckBox))
				{
					CheckBox c = (CheckBox)SelectedControl;
					c.Font = SelectedFont;
					c.ForeColor = SelectedForeColor;
				}
				else if (foundControl.GetType() == typeof(DateTimePicker))
				{
					DateTimePicker c = (DateTimePicker)SelectedControl;
					c.Font = SelectedFont;
					c.ForeColor = SelectedForeColor;
				}
			}
		}

		#endregion

		#region Events
		/// <summary>
		/// Dirty Event
		/// </summary>
		public event EventHandler<DirtyEventArgs> DirtyChanged;
		/// <summary>
		/// Event notification if the AllowEdit Property has changed
		/// </summary>
		public event EventHandler<EventArgs> AllowEditChanged;
		/// <summary>
		/// Properly invokes a delegate allowing for a WinForm thread
		/// </summary>
		/// <param name="del">The delegate to call</param>
		/// <param name="args">Any arguments to be passed</param>
		public static void InvokeDelegate(Delegate del, object[] args)
		{
			System.ComponentModel.ISynchronizeInvoke synchronizer;
			synchronizer = del.Target as System.ComponentModel.ISynchronizeInvoke;
			if (synchronizer != null) //A Windows Forms object
			{
				if (synchronizer.InvokeRequired == false)
				{
					del.DynamicInvoke(args);
					return;
				}
				try
				{
					synchronizer.Invoke(del, args);
				}
				catch
				{ }
			}
			else //Not a Windows Forms object
			{
				del.DynamicInvoke(args);
			}
		}

		/// <summary>
		/// Triggers the event delegate
		/// </summary>
		private void InvokeAllowEditChanged(object sender, EventArgs e)
		{
			if (null != this.AllowEditChanged)
			{
				System.Delegate[] dlgs = this.AllowEditChanged.GetInvocationList();
				if (null != dlgs)
				{
					foreach (Delegate d in dlgs)
					{
						if (null != d)
						{
							InvokeDelegate(d, new object[2] { sender, e });
						}
					}
				}
			}
		}
		/// <summary>
		/// InvokeDirty Event on Clients
		/// </summary>
		/// <param name="sender"></param>
		/// <param name="e"></param>
		private void InvokeDirty(object sender, DirtyEventArgs e)
		{
			if (null != this.DirtyChanged)
			{
				System.Delegate[] dlgs = this.DirtyChanged.GetInvocationList();
				if (null != dlgs)
				{
					foreach (Delegate d in dlgs)
					{
						if (null != d)
						{
							InvokeDelegate(d, new object[2] { sender, e });
						}
					}
				}
			}
		}
		#endregion
	}
	#endregion
}